/**
 * Created by lixiaodi on 2018/10/22.
 */

//表单下拉框数据
var comparison = [
    {id:">",text:"大于"},
    {id:"<",text:"小于"},
    {id:"=",text:"等于"},
    {id:"!=",text:"不等于"},
    {id:">=",text:"大于等于"},
    {id:"<=",text:"小于等于"},
    {id:"null",text:"为空"},
    {id:"!null",text:"不为空"}
];
var editType = [
	{id:"1",text:"文本"},
    {id:"2",text:"日期"},
    {id:"3",text:"文本区间"},
    {id:"4",text:"日期区间"},
    {id:"5",text:"下拉框选项"}
];
var childData;
mini.parse();
//得到form元素
var form = new mini.Form("sqlForm");
var saveBtn = mini.get("saveBtn");
var buttonEdit = mini.get("buttonEdit");
var DataSource = mini.getByName("dataSource");
var reportIds;
//按钮事件
var btnEvent = {
    //保存
    save : function(){
        var ids = form.getData();
        console.log(ids);
        var condition = {
            reportId : reportIds,
            seachId : ids.seachId,
            name : ids.name,
            title : ids.title,
            type : ids.type,
            compare : ids.compare,
            DataSource : ids.dataSource,
            ifCommon : ids.ifCommon,
            ifBetween : ids.ifBetween,
            createDate : ids.createDate,
            updateDate : ids.updateDate
        }
        console.log(condition)
        var jsonString = JSON.stringify(condition);
        console.log(jsonString)
        $.ajax({
            url : ajaxUrl + "/seachParamController/saveOrUpdate.do",
            type : "POST",
            contentType:"application/json",
            data : jsonString,
            success : function(data){
                console.log(data.data);
                reporParam = data;
                CloseWindow("save");
            },
            error : function(data){
                alert("失败");
                CloseWindow();
            }
        });
    },
    //弹出参照
    buttonEdit : function(){
        mini.open({
            url     : bootPATH+"../frame/frame1/pages/reportForm/filedList.html",
            title   : "数据源列表",
            width   : 600,
            height  : 400,
            onload  : function () {
                var iframe = this.getIFrameEl();
                var data = { action: "edit", id: reportIds };
                iframe.contentWindow.SetData(data);
            },
            ondestroy: function (action) {
                if (action == "ok") {
                    var iframe = this.getIFrameEl();
                    var data = iframe.contentWindow.GetData();
                    data = mini.clone(data);    //必须
                    if (data) {
                        DataSource.setValue(data.column_name);
                        DataSource.setText(data.column_name);
                    }
                }

            }
        });
    }
}
//添加事件监听
saveBtn.on("click",btnEvent.save);
buttonEdit.on("click",btnEvent.buttonEdit);
//
//赋值
function SetData(data) {
    //跨页面传递的数据对象，克隆后才可以安全使用
    data = mini.clone(data);
    //判断状态是编辑还是新增
    if (data.action == "edit") {
        reportIds = data.reportId;
        var searchId = data.id;
        $.ajax({
            url: ajaxUrl + "/seachParamController/FindSerchParam.do?id=" + searchId,
            type: "POST",
            success: function (data) {
                var o = mini.decode(data);
                form.setData(o.data);
                form.setChanged(false);
            },
            error : function(data){
                console.log(data);
            }
        });
    }else if(data.action == "new"){
        reportIds = data.reportId;
    }
}

function GetData() {
    var o = form.getData();
    console.log(o);
    return reporParam.data;
}
function CloseWindow(action) {
    if (action == "close" && form.isChanged()) {
        if (confirm("数据被修改了，是否先保存？")) {
            return false;
        }
    }
    if (window.CloseOwnerWindow) return window.CloseOwnerWindow(action);
    else window.close();
}
function onOk(e) {
    SaveData();
}
function onCancel(e) {
    CloseWindow("cancel");
}